﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DCTLib.Common
{
    public enum DatetimeFormat
    {
        ddMMyyyy,
        MMddyyyy,
        yyyyMMdd
    }

    public class DateTimeConst
    {
        public const string CON_FORMAT_ddMMyyyy = "dd/MM/yyyy";
        public const string CON_FORMAT_MMddyyyy = "MM/dd/yyyy";
        public const string CON_FORMAT_yyyyMMdd = "yyyy/MM/dd";
    }

    /// <summary>
    /// Class provides methods for working with datetime.
    /// </summary>
    public class DatetimeHelper
    {
        #region DateTime
        private DatetimeFormat format;
        private string delimiter = "/";
        #endregion

        #region Constructors/Destructors
        public DatetimeHelper(DatetimeFormat format, string delimiter)
        {
            this.format = format;
            this.delimiter = delimiter;
        }
        #endregion

        #region Methods
        #region Public methods
        public int CompareDate(string strFromDate, string strToDate)
        {
            int Year = 0;
            int Month = 0;
            int Day = 0;

            Year = GetYear(strFromDate);
            Month = GetMonth(strFromDate);
            Day = GetDay(strFromDate);

            DateTime fromDate = new DateTime(Year, Month, Day, 0, 0, 0, 0);

            Year = GetYear(strToDate);
            Month = GetMonth(strToDate);
            Day = GetDay(strToDate);

            DateTime toDate = new DateTime(Year, Month, Day, 0, 0, 0, 0);

            return fromDate.CompareTo(toDate);
        }

        public int GetDay(string input)
        {
            string[] values = StringHelper.Split(input.Trim(), delimiter);
            string strReturn = "";

            switch (format)
            {
                case DatetimeFormat.ddMMyyyy:
                    strReturn = values[0];
                    break;
                case DatetimeFormat.MMddyyyy:
                    strReturn = values[1];
                    break;
                case DatetimeFormat.yyyyMMdd:
                    strReturn = values[2];
                    break;
            }

            return Convert.ToInt32(strReturn);
        }

        public int GetMonth(string input)
        {
            string[] values = StringHelper.Split(input.Trim(), delimiter);
            string strReturn = "";

            switch (format)
            {
                case DatetimeFormat.ddMMyyyy:
                    strReturn = values[1];
                    break;
                case DatetimeFormat.MMddyyyy:
                    strReturn = values[0];
                    break;
                case DatetimeFormat.yyyyMMdd:
                    strReturn = values[1];
                    break;
            }

            return Convert.ToInt32(strReturn);
        }

        public int GetYear(string input)
        {
            string[] values = StringHelper.Split(input.Trim(), delimiter);
            string strReturn = "";

            switch (format)
            {
                case DatetimeFormat.ddMMyyyy:
                    strReturn = values[2];
                    break;
                case DatetimeFormat.MMddyyyy:
                    strReturn = values[2];
                    break;
                case DatetimeFormat.yyyyMMdd:
                    strReturn = values[0];
                    break;
            }

            return Convert.ToInt32(strReturn);
        }


        public string ConvertDateString(string input, DatetimeFormat inputFormat, DatetimeFormat outputFormat)
        {
            string[] values = StringHelper.Split(input.Trim(), delimiter);

            string year = "";
            string month = "";
            string day = "";

            switch (inputFormat)
            {
                case (DatetimeFormat.ddMMyyyy):
                    year = values[2];
                    month = values[1];
                    day = values[0];
                    break;
                case (DatetimeFormat.MMddyyyy):
                    year = values[2];
                    month = values[0];
                    day = values[1];
                    break;
                case (DatetimeFormat.yyyyMMdd):
                    year = values[0];
                    month = values[1];
                    day = values[2];
                    break;
            }

            string strReturn = "";
            switch (outputFormat)
            {
                case (DatetimeFormat.ddMMyyyy):
                    strReturn = day + delimiter + month + delimiter + year;
                    break;
                case (DatetimeFormat.MMddyyyy):
                    strReturn = month + delimiter + day + delimiter + year;
                    break;
                case (DatetimeFormat.yyyyMMdd):
                    strReturn = year + delimiter + month + delimiter + day;
                    break;
            }

            return strReturn;
        }

        /// <summary>
        /// getdate from a string
        /// </summary>
        /// <param name="startDate"></param>
        /// <returns></returns>
        public static DateTime ConvertDateString(string sDate)
        {
            try
            {
                sDate = sDate.Trim().Replace("/", "-");
                return new DateTime(int.Parse(sDate.Substring(sDate.LastIndexOf("-") + 1)), int.Parse(sDate.Substring(sDate.IndexOf("-") + 1, sDate.LastIndexOf("-") - sDate.IndexOf("-") - 1)), int.Parse(sDate.Substring(0, sDate.IndexOf("-"))));
            }
            catch (Exception ex)
            {
                return DateTime.Now;
            }
        }

        /// <summary>
        /// Conversion from standard format(mm/dd/yyyy) to custom format: dd/MM/yyyy
        /// </summary>
        /// <param name="startDate"></param>
        /// <returns></returns>
        public static string ToDateDdMMyyyy(DateTime dtime)
        {
            try
            {
                return dtime.ToString(DateTimeConst.CON_FORMAT_ddMMyyyy);
            }
            catch (Exception ex)
            {
                return DateTime.Now.ToString(DateTimeConst.CON_FORMAT_ddMMyyyy);
            }
        }
        #endregion
        #endregion
    }
}
